package com.erp.module.crm.xXCrm

import android.annotation.SuppressLint
import androidx.appcompat.app.AlertDialog
import android.util.Log
import android.view.View
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.activityKView.CrmFKView
import com.erp.bean.ClientContractBean
import com.erp.myFragment.BaseKFragment
import com.erp.util.Dialogutil
import com.erp.util.Httputil
import com.erp.util.Myutil
import com.erp.wheelview.DateUtils
import kotlinx.android.synthetic.main.fragment_clientfour.*
import org.json.JSONObject

/**
 * Created by Administrator on 2016/11/2 0002.
 * 合同
 */
@SuppressLint("ValidFragment")
class FragmentClientfour : BaseKFragment, View.OnClickListener {
    lateinit var clientContractBean: ClientContractBean ;
    lateinit var FKP : CrmFKView;
    constructor(){
    }
    constructor( clientContractBean: ClientContractBean, FKP: CrmFKView){
        this.clientContractBean=clientContractBean
        this.FKP = FKP
    }
    lateinit var updatedialog: AlertDialog
    override fun getLayoutId(): Int {
        fragmentname = f
        return R.layout.fragment_clientfour
    }
    override fun initData() {
        initView()
        initdata()

    }
    private fun initView() {
        BtBaocun.setOnClickListener(this)
        LlContractRegistrationTime.setOnClickListener(this)
        LlContractPromisDeliveryTime.setOnClickListener(this)
        LlContractYudinganzhuangTime.setOnClickListener(this)
    }

    private fun initdata() {
        if(clientContractBean.contract_num == ""){
            TvHetongtou.visibility = View.VISIBLE
            TvHetongtou.text = DateUtils.currenNianYue()
        }else{
            TvHetongtou.visibility = View.GONE
            EtContractContractNum.hint = "输入三位数合同号"
        }
        Myutil.settv(clientContractBean.contract_num, EtContractContractNum)
        Myutil.settv(Myutil.getdata(clientContractBean.registration_time), TvContractRegistrationTime)
        Myutil.settv(clientContractBean.amount, EtContractAmount)
        Myutil.settv(Myutil.getdata(clientContractBean.promis_delivery_time), TvContractPromisDeliveryTime)
        Myutil.settv(Myutil.getdata(clientContractBean.yudinganzhuang_time), TvContractYudinganzhuangTime)
        Myutil.settv(clientContractBean.reason, EtContractReason)
        Myutil.settv(clientContractBean.remark, EtContractRemark)
    }

    override fun onClick(v: View?) {
        when(v){
            BtBaocun -> {
                val jo=xiugai()
                updatedialog= Myutil.showupdatedialog(context, "正在保存", false)
                Httputil.cleanclient(activity, ClientKActivity.getid(),jo,object : OnUserDataInterface {
                    override fun onUserData(data: String?) {
                        updatedialog.dismiss()
                        if(data==null){
                            Myutil.MyTost("保存失败")
                            return
                        }
                        Myutil.MyTost("保存成功")
                        FKP.updata(data)
                    }

                    override fun onNot(throwable: String?) {
                        updatedialog.dismiss()
                        Log.d("TAAAG","throwable="+throwable)
                    }

                })
            }
            LlContractRegistrationTime -> {
                Dialogutil.showBottoPopupWindow(TvContractRegistrationTime, "选择登记时间", TvContractRegistrationTime.text.toString(), activity)
            }
            LlContractPromisDeliveryTime -> {
                Dialogutil.showBottoPopupWindow(TvContractPromisDeliveryTime, "选择交付时间", TvContractPromisDeliveryTime.text.toString(), activity)
            }
            LlContractYudinganzhuangTime -> {
                Dialogutil.showBottoPopupWindow(TvContractYudinganzhuangTime, "选择安装时间", TvContractYudinganzhuangTime.text.toString(), activity)
            }
        }
    }

    private fun xiugai(): JSONObject {
        var jo = JSONObject()
        try {
            val contract = JSONObject()
            contract.put("contract_num", EtContractContractNum.text.toString())
            contract.put("registration_time", TvContractRegistrationTime.text.toString())
            contract.put("amount", EtContractAmount.text.toString())
            contract.put("promis_delivery_time", TvContractPromisDeliveryTime.text.toString())
            contract.put("yudinganzhuang_time", TvContractYudinganzhuangTime.text.toString())
            contract.put("reason", EtContractReason.text.toString())
            contract.put("remark", EtContractRemark.text.toString())
            jo.put("contract", contract)
        } catch (e: Exception) {
            e.printStackTrace()
            Log.d("TAAAG", "--------------" + e.toString())
        }

        return jo
    }
    fun notifydata() {
        initdata()
    }

    companion object {
        private val f = "FragmentClientfour"
    }
}
